package com.shopping.mall.auth.utils;


import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

public class UserUtils {
    /**
     * 获取当前登录的用户
     *
     * @return User
     */
//    public static User getLoginUser() {
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        org.springframework.security.core.userdetails.User principal = (org.springframework.security.core.userdetails.User) authentication.getPrincipal();
//        return new User(principal.getUsername());
//    }

    /**
     * 获取当前登录的用户ID
     *
     * @return String
     */
    public static String getLoginUserId() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            User principal = (User) authentication.getPrincipal();
            return principal.getUsername();
        } catch (Exception e) {
            return "unLogin";
        }
    }
}
